/*
 * CharNGramEstEmpirical.java
 *
 * Created on November 8, 2007, 9:54 AM
 *
 */
package EstimatorPackage.NGramChar;

import EstimatorPackage.Consts;

/**
 *
 * @author Martin Haulrich
 */
public class CharNGramEstEmpirical extends AbstractCharNGramEst {

    /**
     * Constructor.
     * Make new empirical estimator on the base of the ngrams in <code>ngrams</code>.
     * Only allow for estimates of <code>N</code>-grams. This should not be set higher than the N in <code>ngrams</code>.
     * @param ngrams N-grams that the estimate is based on.
     * @param N Estimates are made for up to N-grams.
     */
    public CharNGramEstEmpirical(CharacterNGramCount ngrams, int N) {
        super(ngrams, N);
    }


    @Override
    public double getEstimate(String ngram) {
        if (ngram.length() > N) {
            System.err.println("Warning: this estimator only has up to " + N + "-grams.");
            System.err.println("Returning 0-probability");
            return Math.log10(0);
        }
        //     System.err.println(ngram + ": " + ngrams.getCount(ngram));
        int count = ngrams.getCount(ngram);
        // To avoid division by zero in next calculation.
        if (count == 0) {
            return Consts.logZero;
        }
        //      System.err.println(count + " " + ngrams.getNCount(ngram.length()));
        return Math.log10((double) count / ngrams.getNCount(ngram.length()));
    }
}
